package com.dawnClass.service.impl;

import com.alibaba.fastjson.JSON;
import com.dawnClass.mapper.CourseMapper;
import com.dawnClass.pojo.Course;
import com.dawnClass.service.CourseESService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//业务编写
@Service
public class CourseESServiceImpl implements CourseESService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private CourseMapper courseMapper;

    //1.解析数据放入es索引中
    public Boolean parseContent(String keywords) throws IOException {
        List<Course> courseList = courseMapper.selectByExample(null);
        //把查询到的数据放入es中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
            for (int i = 0;i < courseList.size();i++){
                bulkRequest.add(new IndexRequest("course").source(JSON.toJSONString(courseList.get(i)), XContentType.JSON));
            }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }
    //2.获取这些数据实现搜索功能
    public List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException{
        if (pageNo<=1){
            pageNo = 1;
        }
        System.out.println(keyword);
        //条件搜索
        SearchRequest searchRequest = new SearchRequest("course");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //精准匹配
//        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("courseSort", keyword);
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("courseSort", keyword);
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("courseSort", keyword);
//        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //解析结果
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit documentFields : searchResponse.getHits().getHits()){
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }
}
